package org.dtrd.modules.record.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.record.entity.bean.MedicalListInfo;
import org.dtrd.modules.record.entity.bean.MedicalRecordInfo;
import org.dtrd.modules.record.entity.request.TimePeriodRequest;
import org.dtrd.modules.record.service.IMedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_SAVE_OR_UPDATE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * @author qr
 * @date 2021/12/6 10:52
 */
@Api(tags = "药品打卡")
@Slf4j
@RestController
@RequestMapping("/101/04/")
public class MedicalController extends BaseController {

    @Autowired
    private IMedicalRecordService medicalRecordService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("保存或修改药品记录")
    @PostMapping("1010401")
    @AutoLog(value="保存或修改药品记录", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateMedicalRecord(@RequestBody MedicalRecordInfo medicalRecordInfo) {
        if (isIntParamInvalid(medicalRecordInfo.getPatientId())
                || medicalRecordInfo.getMedicalRecordType() == null) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = medicalRecordService.saveOrUpdateMedicalRecord(medicalRecordInfo);
        String logContent = String.format("保存或修改药品记录，患者id：%d, 记录时间：%s，记录时间类型：%s", medicalRecordInfo.getPatientId(),
                medicalRecordInfo.getRecordTime(), medicalRecordInfo.getMedicalRecordType());
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("分页获取药品列表")
    @PostMapping("1010402")
    @AutoLog(value="获取药品列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getMedicalList(@RequestBody TimePeriodRequest request) {
        if (isIntParamInvalid(request.getPatientId())) {
            return error(STATE_PARAMS_MISSING);
        }
        String startDate = null;
        String endDate = null;
        String[] timePeriod = handleTimePeriod(request);
        if (timePeriod != null) {
            startDate = timePeriod[0];
            endDate = timePeriod[1];
        }
        if (startDate == null || endDate == null) {
            return error(STATE_PARAMS_MISSING);
        }
        Integer pageNum = getIntParam(request.getPage(), 1);
        Integer pageSize = getPageSize(request.getPageSize(), 20);
        MedicalListInfo medicalListInfo = medicalRecordService.getMedicalRecordList(pageNum, pageSize, request.getPatientId(), startDate, endDate);
        String logContent = String.format("分页获取药品列表，查询患者id：%d", request.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(medicalListInfo);
    }
}
